home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
AI88PATT
/
PATHNAME.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-06
|
5KB
|
223 lines
/* The functions getfile() and putfile use a Standard File GetFile or
PutFile Dialog Box to retrieve the full path of a TEXT file which can
be used a the file name argument to fopen.
Both getfile(char* filename) and
putfile(char* filename,char* prompt,char* defaultfile) return
the full path of the file in the argument 'filename'.
The int return value is the number of characters in the name. Zero is
returned if the file name couldn't be fetched and -1 is returned if
the path name has more than 255 chars. `prompt' and `defaultfile' are
a titling prompt string and a filename to be highlighted in the
putfile() dialog box.
Written by Jeff Meredith 12/88. Thanks to Mike Prange for his encouragement
to add functionality to getfile() and for the MacTutor Loan.
Getfile(), Putfile(), and Pathname() were streamlined by Prange, dec. 1988.
Usage: [link with MacTraps]
***************************************************/
#include <stdio.h>
/*main()
{
char name1[255],name2[255];
FILE *fp1,*fopen();
if(getfile(name1) != 0)
fp1 = fopen(name1,"r");*/
/* ... */
/* fclose(fp1);
if(putfile(name2,"Name of output file:","Output.out") != 0);
fp1 = fopen(name2,"w");*/
/* ... */
/* fclose(fp1);
printf("%s\n%s\n",name1,name2);
}*/
/***************************************************
*/
#include <stdio.h>
#include <HFS.h>
#include <WindowMgr.h>
#include <StdFilePkg.h>
#include <pascal.h>
int getfile(result)
char *result;
{
SFReply reply;
SFTypeList myTypes; /* see StdFilePkg.h */
Point SFwhere;
SFwhere.h = 100; /* coordinates for dialog box */
SFwhere.v = 50;
/* if(thePort == NULL){
Init_stdio();
InitGraf(&thePort);
InitFonts();
InitWindows();
}*/
myTypes[0]='TEXT';
SFGetFile( SFwhere, "\p", 0L, 1, myTypes, 0L, &reply );
if (reply.good) {
char buff[255],*p,*pathname();
register int i;
p = PtoCstr((char *) reply.fName);
for(i = 0; buff[i] = p[i]; ++i) /* copy name into a buffer capable of */
; /* holding the full path */
if(!pathname(buff,reply.vRefNum)) /* return -1 for path > 255 */
return -1;
for(i = 0; result[i] = buff[i]; ++i)
;
return i; /* return number of chars in pathname */
}
else
return 0; /* return 0 if bad reply */
}
int putfile(result,prompt,defaultname)
char *result,*prompt,*defaultname;
{
SFReply reply; /* Standard File Reply see StdFilePkg.h*/
int wdRefNum; /* working directory reference Number */
SFTypeList myTypes;
Point SFwhere;
SFwhere.h = 100; /* coordinates for dialog box */
SFwhere.v = 50;
/*if(thePort == NULL){ Initializations
InitGraf(&thePort);
InitFonts();
InitWindows();
}*/
/*myTypes[0]='TEDC';*/
SFPutFile( SFwhere, CtoPstr(prompt), CtoPstr(defaultname),0L, &reply );
if (reply.good) {
char buff[255],*p,*pathname();
register int i;
p = PtoCstr((char *) reply.fName);
for(i = 0; buff[i] = p[i]; ++i) /* copy name into a buffer capable of */
; /* holding the full path */
if(!pathname(buff,reply.vRefNum)) /* return -1 for path > 255 */
return -1;
for(i = 0; result[i] = buff[i]; ++i)
;
return i; /* return number of chars in pathname */
}
else
return 0; /* return 0 if bad reply */
}
/* modified from Mike Schuster, "Programming for HFS Compatibility ", the
Complete MacTutor, Vol. 2, pg. 87. (Mac C) to Light Speed C, Jeff Meredith
12/88
Changes.
1. removed requirement to pass size of wdrefnum(sizeof(wdrefnum)) as the
third parameter.
2. removed need for strntac() which is not in LSC libraries.
3. minor typecasting changes to match LSC prototypes.
4. FSFCBLen defined in HFS.h, no need to define here */
#define mfsSigWord 0xd2d7
#define hfsSigWord 0x4244
#define rootDirID 2
/*#define ioDirFlg 0x10*/
char *pathname(pname,wdrefnum)
char *pname;
int wdrefnum;
{
HVolumeParam vp;
WDPBRec wp;
DirInfo dp;
Str255 dname;
char *strtac();
int length;
/* initialize params */
vp.ioNamePtr=(StringPtr)&dname;
vp.ioVRefNum=wdrefnum;
vp.ioVolIndex=0;
wp.ioNamePtr=0L;
wp.ioVRefNum=wdrefnum;
wp.ioWDIndex=0;
wp.ioWDProcID=0L;
dp.ioNamePtr=(StringPtr)&dname;
dp.ioFDirIndex=-1;
/* get volume information */
if(!PBHGetVInfo(&vp,0))
/* if MFS or HFS root, return volume name */
if(FSFCBLen==-1||vp.ioVSigWord==mfsSigWord||vp.ioVRefNum==wdrefnum)
pname = PtoCstr((Ptr)vp.ioNamePtr);
/*get working directory information */
else if (!PBGetWDInfo(&wp,0)){
/* traverse path from working directory to root */
dp.ioVRefNum=wp.ioWDVRefNum;
dp.ioDrParID=wp.ioWDDirID;
for(length = 0; pname[length]; ++length) /* compute length of pname */
;
do{
char *folder;
int i;
/* get next node information */
dp.ioDrDirID=dp.ioDrParID;
if(PBGetCatInfo(&dp,0))
break;
/*concatenate node name to result*/
folder = PtoCstr((char *) dp.ioNamePtr);
for(i = 0; folder[i]; ++i) /* compute length of folder */
;
if((length += i) > 255)
return NULL;
pname = strtac(":",pname);
pname = strtac(folder,pname);
}while(dp.ioDrDirID!=rootDirID);
}
return pname;
}
char *strtac(s1,s2)
/*
concatenate s1 and s2 and put in s2
*/
register char *s1,*s2;
{
register int i;
char buff[256];
register char *p;
p = buff;
for(i = 0; p[i] = s2[i]; ++i)
;
for(i = 0; s2[i] = s1[i]; ++i)
;
for(; s2[i] = *p++; ++i)
;
return s2;
}